/* wocnppv.p - WORK ORDER ISSUE OF CONSIGNED PO MATERIAL  - PPV CALC        */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/* $Revision: 1.7.3.3 $                                                            */

/*----------------------------------------------------------------------
 * Purpose:  This program creates the gl transaction for purchase price
 *           variances for consigned material at usage time.
 * Notes:
 *---------------------------------------------------------------------*/

/* Revision: 1.7    BY: Ellen Borden   DATE: 04/02/02  ECO: *P017* */
/* Revision: 1.7.3.1  BY: Gnanasekar     DATE: 06/27/03  ECO: *P0WB* */
/* Revision: 1.7.3.2  BY: Dipesh Bector  DATE: 08/28/03  ECO: *P11R* */
/* $Revision: 1.7.3.3 $      BY: Dipesh Bector  DATE: 08/31/03  ECO: *P11Z* */

/*V8:ConvertMode=NoConvert                                                  */

/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */


{mfdeclre.i}


define input parameter ip_order       like pod_nbr      no-undo.
define input parameter ip_part        like pt_part      no-undo.
define input parameter ip_site        like cnsix_site   no-undo.
define input parameter ip_usage_qty   as decimal        no-undo.
define input parameter ip_project     like trgl_cr_proj no-undo.
define input parameter base_amt       as decimal        no-undo.
define input parameter ip_trnbr       like tr_trnbr     no-undo.
define input parameter ip_accrue_tax  like mfc_logical  no-undo.
define input parameter ip_receiver    like prh_receiver no-undo.
define input parameter ip_po_line     like pod_line     no-undo.
define input parameter glx_mthd       like cs_method    no-undo.

define new shared variable ref like glt_ref  no-undo.
define new shared variable eff_date like glt_effdate no-undo
                                         initial today.

define variable sct_recno as recid no-undo.
define variable l_extbase_amt like trgl_gl_amt   no-undo.
define variable ppv_amt         as decimal       no-undo.
define variable mc-error-number like msg_nbr     no-undo.
define variable same-ref        like mfc_logical no-undo.
define variable tax_amt         as decimal       no-undo.
define variable l_recov_taxamt  as decimal       no-undo.

for first gl_ctrl
   fields (gl_can gl_rcptx_acct gl_rcptx_cc gl_rnd_mthd gl_vat)
   no-lock:
end. /*FOR FIRST GL_CTRL */

{gpsct06.i &part=ip_part &site=ip_site &type=""GL"" }
 sct_recno = recid(sct_det).

for first icc_ctrl
   fields (icc_cogs icc_gl_set icc_gl_sum icc_gl_tran icc_mirror)
   no-lock:
end. /* FOR FIRST ICC_CTRL */

if available icc_ctrl then
   same-ref = icc_gl_sum.

for first sct_det
   fields (sct_cst_tot sct_ovh_tl)
   where sct_recno = recid(sct_det) no-lock:
end. /*FOR FIRST SCT_DET */

if glx_mthd = "std"
then
   ppv_amt = ip_usage_qty * (sct_cst_tot - sct_ovh_tl).

/* ROUND PER BASE CURRENCY ROUND METHOD */
{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output ppv_amt,
     input gl_rnd_mthd,
     output mc-error-number)"}

l_extbase_amt = ip_usage_qty * base_amt.
{gprunp.i "mcpl" "p" "mc-curr-rnd"
    "(input-output l_extbase_amt,
      input gl_rnd_mthd,
      output mc-error-number)"}

ppv_amt = l_extbase_amt - ppv_amt.

/* ROUND PER BASE CURRENCY ROUND METHOD */
{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output ppv_amt,
     input gl_rnd_mthd,
     output mc-error-number)"}

/*CALCULATE TAXES IF ACCRUE AT USAGE FLAG IS SET TO YES */
if ip_accrue_tax then do:
   for first tx2d_det no-lock
      where tx2d_ref = ip_receiver and
            tx2d_nbr = ip_order    and
            tx2d_line = ip_po_line and
            tx2d_tr_type = "21":
   end. /*For first tx2d_det*/

   if available tx2d_det
   then
      for first tx2_mstr
         fields (tx2_ap_acct tx2_ap_sub tx2_ap_cc tx2_tax_code)
         where tx2_tax_code = tx2d_tax_code
      no-lock:
      end. /* FOR FIRST tx2_mstr */

   if available tx2d_det then do:
      for first prh_hist no-lock
         where prh_receiver = ip_receiver and
               prh_nbr = ip_order and
               prh_line = ip_po_line:
      end. /*For first prh_hist*/

      /* SEPARATE THE RECOVERABLE AND NON-RECOVERABLE TAX AMOUNTS */

      assign
         tax_amt        = ((tx2d_tax_amt - tx2d_recov_amt) /
                          (prh_rcvd * prh_um_conv)) * ip_usage_qty
         l_recov_taxamt = (tx2d_recov_amt /
                          (prh_rcvd * prh_um_conv)) * ip_usage_qty.

      /* ROUND PER BASE CURRENCY ROUND METHOD */
      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output tax_amt,
           input gl_rnd_mthd,
           output mc-error-number)"}

      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output l_recov_taxamt,
           input gl_rnd_mthd,
           output mc-error-number)"}

      ppv_amt = ppv_amt + tax_amt.

   end. /*If available tx2d_det*/
end. /*If ip_accrue_tax*/

/*CREATE A GL TRANSACTION IF THE PPV IS NOT EQUAL TO ZERO*/

if ppv_amt <> 0 then do:

   /*PPV RECEIPT*/

   /*Get product line from part master*/
   for first pt_mstr
      fields (pt_prod_line)
      where pt_part = ip_part
      no-lock:
   end.

   for first pl_mstr
      fields (pl_ppv_acct pl_ppv_sub pl_ppv_cc
              pl_prod_line pl_rcpt_acct pl_rcpt_sub pl_rcpt_cc)
      where pl_prod_line = pt_prod_line no-lock:
   end. /* FOR FIRST PL_MSTR  */
   create trgl_det.
   assign
      trgl_trnbr = ip_trnbr
      trgl_sequence = recid(trgl_det)
      trgl_dr_acct = pl_ppv_acct
      trgl_dr_sub  = pl_ppv_sub
      trgl_dr_cc   = pl_ppv_cc
      trgl_dr_proj = ip_project
      trgl_cr_acct = pl_rcpt_acct
      trgl_cr_sub  = pl_rcpt_sub
      trgl_cr_cc   = pl_rcpt_cc
      trgl_cr_proj = ip_project.

      if recid(trgl_det) = -1 then .
      assign
         trgl_gl_amt  = ppv_amt
         trgl_type = "RCT-PO".

         for first si_mstr
            fields (si_entity si_site)
            where si_site = ip_site
         no-lock:
         end. /* FOR FIRST si_mstr */

         /* REPLACED 12TH PARAMETER ip_site BY si_entity */

         {mficgl02.i
            &gl-amount=ppv_amt
            &tran-type=trgl_type
            &order-no=ip_order
            &dr-acct=trgl_dr_acct
            &dr-sub=trgl_dr_sub
            &dr-cc=trgl_dr_cc
            &drproj=trgl_dr_proj
            &cr-acct=trgl_cr_acct
            &cr-sub=trgl_cr_sub
            &cr-cc=trgl_cr_cc
            &crproj=trgl_cr_proj
            &entity=si_entity
            &find="false"
            &same-ref="same-ref"
         }

end. /*If ppv_amt <> 0*/

/*CREATE A GL TRANSACTION IF THE RECOVERABLE TAX IS NOT EQUAL TO ZERO*/

if l_recov_taxamt <> 0
then do:

   /*GET PRODUCT LINE FROM PART MASTER*/

   for first pt_mstr
      fields (pt_prod_line)
      where pt_part = ip_part
      no-lock:
   end. /* FOR FIRST pt_mstr */

   for first pl_mstr
      fields (pl_ppv_acct  pl_ppv_sub   pl_ppv_cc
              pl_prod_line pl_rcpt_acct pl_rcpt_sub pl_rcpt_cc)
      where pl_prod_line = pt_prod_line no-lock:
   end. /* FOR FIRST PL_MSTR  */

   create trgl_det.
   assign
      trgl_trnbr    = ip_trnbr
      trgl_sequence = recid(trgl_det)
      trgl_dr_acct  = tx2_ap_acct
      trgl_dr_sub   = tx2_ap_sub
      trgl_dr_cc    = tx2_ap_cc
      trgl_dr_proj  = ip_project
      trgl_cr_acct  = pl_rcpt_acct
      trgl_cr_sub   = pl_rcpt_sub
      trgl_cr_cc    = pl_rcpt_cc
      trgl_cr_proj  = ip_project.

   if recid(trgl_det) = -1
   then .

   assign
      trgl_gl_amt  = l_recov_taxamt
      trgl_type    = "RCT-PO".

   for first si_mstr
      fields (si_entity si_site)
      where si_site = ip_site
   no-lock:
   end. /* FOR FIRST si_mstr */

   {mficgl02.i
      &gl-amount=l_recov_taxamt
      &tran-type=trgl_type
      &order-no=ip_order
      &dr-acct=trgl_dr_acct
      &dr-sub=trgl_dr_sub
      &dr-cc=trgl_dr_cc
      &drproj=trgl_dr_proj
      &cr-acct=trgl_cr_acct
      &cr-sub=trgl_cr_sub
      &cr-cc=trgl_cr_cc
      &crproj=trgl_cr_proj
      &entity=si_entity
      &find="false"
      &same-ref="same-ref"
   }

end. /* IF l_recov_taxamt <> 0 */
